<template>
  <div class="app-container calendar-list-container">
    <div v-if="!opShow && !priceShow">
      <search :list="queryList" :queryEntity="queryEntity"></search>
      <buttons :showQuery="true" :showBack="false" @query="doQuery" :list="btnList" :disabled="disabled" placeholder="加工户名称" :permissions="permissions" @add="doAdd"></buttons>
      <grid :columns="columns" :multi="false" :url="queryUrl" ref="mainGrid" height="auto" diffHeight="105" @change="rowCilck" :queryEntity="queryEntity" :pagination="true"
        :permissions="permissions" @edit="doEdit" @view="doView" @open="doOpen" @close="doClose" @price="doPrice"></grid>
    </div>
    <op ref="op" :visible="opShow" @cancel="cancel"></op>
    <price ref="price" :visible="priceShow" @cancel="cancel"></price>

  </div>
</template>

<script>
import request from '@/router/axios'
import url from '@/const/apiURL'
import grid from '@/components/Grid'
import buttons from '@/components/Buttons'
import op from './op'
import price from './price'
import { Message, MessageBox } from 'element-ui'
import { mapGetters } from 'vuex'
import { YesOrNoFormatter } from '@/const/dict'
import search from '@/components/Search'
import { dict } from '@/const/dict'

export default {
  components: { grid, buttons, op, search, price },
  data () {
    return {
      queryUrl: url.CUSTOMER_LIST_URL,
      disabled: true,
      opShow: false,
      priceShow: false,
      customerCode: null,
      queryEntity: { customerType: 'HANDLE' },
      columns: [
        {
          label: '操作', width: 200, prop: 'op', align: 'center', template: 'op', opList: [
            { name: '编辑', clickName: 'edit', auth: 'handle_edit' },
            { name: '查看', clickName: 'view', auth: 'handle_view' },
            { name: '开启', clickName: 'open', auth: 'handle_open', confirm: true, valueProp: "status", showValue: [0] },
            { name: '关闭', clickName: 'close', auth: 'handle_close', confirm: true, valueProp: "status", showValue: [1] },
            { name: '价格配置', clickName: 'price', auth: 'yes' }
          ]
        },
        { label: '客户名称', width: 260, prop: 'customerName', align: 'left' },
        { label: '联系人', width: 120, prop: 'linkName', align: 'left' },
        { label: '联系电话', width: 160, prop: 'linkMoblie', align: 'left' },
        { label: '是否启用', width: 80, prop: 'status', align: 'center', formatter: YesOrNoFormatter },
        { label: '创建人', width: 100, prop: 'createUserName', align: 'center' },
        { label: '创建时间', width: 100, prop: 'createTime', align: 'center' }
      ],
      btnList: [
        { name: '新增', type: 'primary', icon: 'edit', click: 'add', auth: 'handle_add', ctr: false },
        // { name: '编辑', type: 'primary', icon: 'edit', click: 'edit', auth: 'handle_edit', ctr: true },
        // { name: '查看', type: 'primary', icon: 'edit', click: 'view', auth: 'handle_view', ctr: true },
        // { name: '价格配置', type: 'primary', icon: 'edit', click: 'price', auth: 'yes', ctr: true },
        // { name: '开启', type: 'primary', icon: 'edit', click: 'open', auth: 'handle_open', ctr: true, confirm: true },
        // { name: '关闭', type: 'primary', icon: 'edit', click: 'close', auth: 'handle_close', ctr: true, confirm: true }
      ],
      queryList: [
        { name: '客户名称', type: 'text', prop: 'customerNameLike', placeholder: "客户名称" },
        { name: '联系人', type: 'text', prop: 'linkName', placeholder: "联系人" },
        { name: '是否启用', type: 'select', prop: 'status', placeholder: "是否启用", values: dict().YesOrNo, label: 'name', value: 'value' }
      ]
    }
  },
  created () {
  },
  computed: {
    ...mapGetters([
      'permissions'
    ])
  },
  methods: {
    doAdd () {
      this.templateOp().then((data) => {
        this.opShow = true
        this.$refs['op'].init('新增', { props: data, customerType: 'HANDLE' }, false, url.CUSTOMER_ADD_URL)
      })
    },
    doEdit (row) {
      // const row = this.$refs.mainGrid.getSelectOne()
      if (row) {
        this.customerViewOp(row.customerCode).then((data) => {
          this.opShow = true
          this.$refs['op'].init('编辑', data, false, url.CUSTOMER_EDIT_URL)
        })
      }
    },
    doView (row) {
      //  const row = this.$refs.mainGrid.getSelectOne()
      if (row) {
        this.customerViewOp(row.customerCode).then((data) => {
          this.opShow = true
          this.$refs['op'].init('查看', data, true)
        })
      }
    },
    doPrice (row) {
      //const row = this.$refs.mainGrid.getSelectOne()
      if (row) {
        this.customerPriceViewOp(row.customerCode).then((data) => {
          this.priceShow = true
          this.$refs['price'].init('价格配置', data, false, url.CUSTOMER_PRICE_ADD_URL)
        })
      }
    },
    doQuery (text) {
      this.$refs['mainGrid'].doQuery(text)
    },
    rowCilck (row) {
      this.disabled = row.length == 0
    },
    cancel () {
      this.opShow = false
      this.priceShow = false
    },
    templateOp () {
      return request({
        url: url.G_TEMPLATE_USABLE_URL,
        method: 'post',
        data: { 'templateType': 'CUSTOMER' },
      })
    },
    customerViewOp (customerCode) {
      return request({
        url: url.CUSTOMER_VIEW_URL,
        method: 'post',
        params: { customerCode },
        data: { customerCode }
      })
    },
    customerPriceViewOp (customerCode) {
      return request({
        url: url.CUSTOMER_PRICE_VIEW_URL,
        method: 'post',
        params: { customerCode },
        data: { customerCode }
      })
    },
    openOp (code) {
      return request({
        url: url.CUSTOMER_OPEN_URL,
        method: 'post',
        data: { 'key': 'key' },
        params: { 'customerCode': code }
      })
    },
    closeOp (code) {
      return request({
        url: url.CUSTOMER_CLOSE_URL,
        method: 'post',
        data: { 'key': 'key' },
        params: { 'customerCode': code }
      })
    },
    doOpen (row) {
      //const row = this.$refs.mainGrid.getSelectOne()
      if (row) {
        this.openOp(row.customerCode).then((data) => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    doClose (row) {
      // const row = this.$refs.mainGrid.getSelectOne()
      if (row) {
        this.closeOp(row.customerCode).then((data) => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
